package com.yuandian.management.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.management.mapper.ExcelBusinessMapper;
import com.yuandian.management.service.IExcelBatchService;
import com.yuandian.management.utils.BusinessExcelReadListener;
import com.yuandian.management.utils.ExcelBatchUtil;
import com.yuandian.management.utils.StepExcelReadListener;
import com.yuandian.management.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 批量导入:经讨论，选择异步方案，先解析excel数据入库，再启定时任务，从表里面读取业务信息后逐条处理。
 * -- 避免程序异常导致项目经理不知道哪些导入，哪些未导入。
 *
 * @author fucx
 * @date 2020/08/28
 */
@Api(tags = "业务步骤批量导入")
@RestController
@RequestMapping("/excel/batch")
public class ExcelBatchController {

	@Autowired
	private IExcelBatchService excelBatchService;

	@Autowired
	private ExcelBusinessMapper excelBusinessMapper;

	/**
	 * 根据开始时间 和 结束时间查询流水信息
	 * @param batch
	 * @return
	 */
	@ApiOperation(value = "根据开始时间 和 结束时间查询流水信息", notes = "根据开始时间 和 结束时间查询流水信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "createTime", value = "开始时间", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "endTime", value = "结束时间", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataType = "Integer"),
	})
	@PostMapping("/getBatchTable")
	public R<Page<ImportInfoVo>> getBatchTable(@RequestBody @ApiIgnore ImportInfoVo batch) {
		return R.ok(excelBatchService.queryBatchList(batch));
	}

	/**
	 * 上传Excel
	 *
	 * @param file file
	 * @return Result
	 * @throws IOException
	 */
	@PostMapping("/upload")
	@ApiOperation(value = "上传Excel", notes = "上传Excel")
	public R upload(@RequestParam("file") MultipartFile file) throws IOException {
		BusinessExcelReadListener businessListener=new BusinessExcelReadListener();
		ExcelBatchUtil.getSheet(file,0, ExcelBusinessVo.class,businessListener);
		List<ExcelBusinessVo> businessList=businessListener.getList();
		StepExcelReadListener stepExcelReadListener=new StepExcelReadListener();
		ExcelBatchUtil.getSheet(file,1, ExcelStepVo.class,stepExcelReadListener);
		List<ExcelStepVo> stepVoList=stepExcelReadListener.getList();
		R result = excelBatchService.resultMap(businessList, stepVoList);
		return result;
	}

	/**
	 *  下载Excel模板
	 * @param response
	 * @throws IOException
	 */
	@GetMapping("/download")
	@ApiOperation(value = "下载Excel模板", notes = "下载Excel模板")
	public void getTemplate(HttpServletResponse response) throws Exception {
		excelBatchService.getTemplate("业务步骤批量导入模板","template/business_step.xlsx",response);
	}


	/**
	 * 根据流水号导出Excel
	 * @param dataNumber
	 * @param response
	 * @throws IOException
	 */
	@ApiOperation(value = "根据流水号导出Excel", notes = "根据流水号导出Excel")
	@GetMapping("/download/{dataNumber}")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "dataNumber", value = "流水号", paramType = "path", required = true, dataType = "String"),
	})
	public void getExcelExport(@PathVariable(value = "dataNumber") String dataNumber, HttpServletResponse response) throws IOException {
		excelBatchService.getExcelExport("导入业务步骤表", dataNumber, response);
	}

	/**
	 * 前端查询，根据流水号获取业务
	 *  dataNumber: 流水号
	 *  pageSize: 页大小
	 *  pageNUm: 页号
	 * @return
	 */
	@ApiOperation(value = "前端查询，根据流水号获取业务", notes = "前端查询，根据流水号获取业务")
	@PostMapping("/getBusinessTable")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "dataNumber", value = "流水号", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataType = "Integer"),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataType = "Integer"),
	})
	public R<Page<ExcelBusiness>> getBatchTable(@RequestBody ImportBusinessResultVo business) {
		Page <ExcelBusiness> page = new Page<>(business.getPageNum(), business.getPageSize());
		return R.ok(excelBusinessMapper.queryBusinessPageByDataNumber(page, business));
	}


	/**
	 * 根据流水号和业务序号查询步骤信息
	 *
	 * @return
	 */
	@PostMapping("/getStepTable")
	@ApiOperation(value = "根据流水号和业务序号查询步骤信息", notes = "根据流水号和业务序号查询步骤信息")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "dataNumber", value = "流水号", paramType = "body", required = true, dataType = "String"),
			@ApiImplicitParam(name = "pageSize", value = "每页大小", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "pageNum", value = "当前页数", paramType = "body", required = true, dataTypeClass = Integer.class),
			@ApiImplicitParam(name = "sequenceNumber", value = "业务序号", paramType = "body", required = true, dataType = "String"),
	})
	public R<Page<ImportStepResultVo>> getBatchTable(@RequestBody ImportStepResultVo step) {
		return R.ok(excelBatchService.queryStepList(step));
	}
}
